/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.directory.mavibot.btree;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.junit.Before;
import org.junit.Test;

/**
 * A class to test the classics LRU cache operations
 *  
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class CacheLRUTestMavibot 
{
	private static CacheLRU cache;
	private static int size;
	private static int nbThreads;
	
	@Before
	public void init()
	{
		size=10000;
		nbThreads=100;
		cache=new CacheLRU(size);
	}
	
	@Test
	public void TestContenentCache()
	{
		int shiftSize=2;
		for( int i=0;i<size+shiftSize;i++ )
		{
			cache.put( i, i );
		}
		for(Integer i=shiftSize;i<size+shiftSize;i++)
		{
			assertEquals(i, cache.get(i));
		}
		for(int i=0;i<shiftSize;i++)
		{
			assertEquals(null, cache.get(i));
		}
	}
	
	@Test
	public void TestMultiThreadCache() throws InterruptedException, IOException
	{
        final CountDownLatch latch = new CountDownLatch( nbThreads );

        for ( int i = 0; i < nbThreads; i++ )
        {
            final Integer prefix = i;
            Thread test = new Thread()
            {
                public void run()
                {
                    try
                    {
                        for ( int j = 0; j < size/nbThreads; j++ )
                        {
                            Integer value = prefix * size/nbThreads + j;
                            cache.put(value, value);
                            sleep(1);
                            cache.get(value-1);
                            sleep(1);
                            cache.getCacheStat();
                        }

                        latch.countDown();
                    }
                    catch ( Exception e )
                    {
                    }
                }
            };

            test.start();
        }

        latch.await();

		for (Integer i = 0; i < size; i++) 
		{
			assertEquals( i, cache.get( i ) );
        }
    }
	
	@Test
	public void testCloseCache() throws InterruptedException, IOException
	{
		final CountDownLatch latch = new CountDownLatch( nbThreads );
        for ( int i = 0; i < nbThreads; i++ )
        {
            final Integer prefix = i;
            Thread test = new Thread()
            {
                public void run()
                {
                    try
                    {
                        for ( int j = 0; j < size/nbThreads; j++ )
                        {
                        	if(!cache.isClose())
                        	{
                        		Integer value = prefix * size/nbThreads + j;
                                cache.put(value, value);
                        	}
                        	else
                        	{
                        		assertEquals(true, cache.isClose());
                        	}
                        }
                        if(!cache.isClose())
                    	{
                    		cache.removeAll();
                    		cache.setClose(true);
                    		assertEquals(true, cache.isClose());
                    	}
                        assertEquals(true, cache.isClose());
                    }
                    catch ( Exception e )
                    {
                    }
                    finally
                    {
                    	latch.countDown();
                    }
                }
            };
            
            test.start();
        }
        
        latch.await();
	}
}

